public class Solution3 {
    public static void main(String[] args) {

        String ss = "ADOBECODEBANC";
        String tt = "ABC";
        System.out.println(minWindow(ss,tt));
    }
    public static String minWindow(String ss, String tt) {
        char[] s = ss.toCharArray();
        char[] t = tt.toCharArray();

        int[] hash1 = new int[128];//统计字符串t中的字符频率
        int[] hash2 = new int[128];//统计窗口中自古的字符频率
        int kinds = 0; //t中字符的种类个数
        for(char ch : t) {
            if(hash1[ch] == 0) {
                kinds++;
            }
            hash1[ch]++;
        }
        int len = Integer.MAX_VALUE;
        int begin = -1;
        for(int left = 0, right = 0, count = 0; right < s.length; right++) {
            //1.进窗口
            char in = s[right];
            hash2[in]++;
            if(hash2[in] == hash1[in]) {
                count++;
            }

            while(count == kinds) {
                //2.更新结果
                if(right - left + 1 < len) {
                    begin = left;
                    len = right - left + 1;
                }
                //3.出窗口
                char out = s[left];
                if(hash2[out] == hash1[out]) {
                    count--;
                }
                hash2[out]--;
                left++;
            }

        }
        if(begin == -1) {
            return new String();
        }
        return ss.substring(begin,begin + len);
    }
}